package com.sls.web.manager.game.service.impl;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.xqt.XQTAccountTypeInfo;
import com.sls.web.manager.game.pojo.xqt.XQTChargetypeInfo;
import com.sls.web.manager.game.pojo.xqt.XQTDiscountInfo;
import com.sls.web.manager.game.pojo.xqt.XQTGameAreaServerInfo;
import com.sls.web.manager.game.pojo.xqt.XQTOrderQueryInfo;
import com.sls.web.manager.game.pojo.xqt.XQTParvalueInfo;
import com.sls.web.manager.game.pojo.xqt.XQTTopInfo;
import com.sls.web.manager.game.querybean.XQTAccountTypeQuery;
import com.sls.web.manager.game.querybean.XQTChargeTypeQuery;
import com.sls.web.manager.game.querybean.XQTDiscountQuery;
import com.sls.web.manager.game.querybean.XQTGameAreaServerQuery;
import com.sls.web.manager.game.querybean.XQTParvalueQuery;
import com.sls.web.manager.game.querybean.XQTTopQuery;
import com.sls.web.manager.game.service.XQTService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Service("xQTService")
public class XQTServiceImpl extends SuperService implements XQTService {

	private Gson gson;

	public XQTServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public void saveOfGoodsTop(XQTTopInfo topInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_SAVE_TOP_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, topInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void saveOfParvalue(XQTParvalueInfo parInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_SAVE_PARVALUE_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, parInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void saveOfChargeType(XQTChargetypeInfo chargeInfo,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_SAVE_CHARGE_TYPE_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, chargeInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void saveOfAccountType(XQTAccountTypeInfo accountInfo,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_SAVE_ACCOUNT_TYPE_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, accountInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void saveOfAreaSrv(XQTGameAreaServerInfo asInfo,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_SAVE_AREA_SERVER_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, asInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void saveOfDiscount(XQTDiscountInfo disInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_SAVE_DISCOUNT_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, disInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<XQTParvalueInfo> queryParvalueInfo(
			XQTParvalueQuery parvalueQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_QUERY_PARVALUE_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, parvalueQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<XQTParvalueInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<XQTParvalueInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public XQTParvalueInfo queryParvalueById(String id, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_QUERY_PARVALUE_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		XQTParvalueInfo parInfo = new XQTParvalueInfo();
		parInfo.setParvalueId(id);
		SocketDTO dto = super.send(head, parInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				XQTParvalueInfo info = gson.fromJson(body,
						XQTParvalueInfo.class);
				return info;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<XQTGameAreaServerInfo> queryAreaServerInfo(
			XQTGameAreaServerQuery asQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_QUERY_AREA_SERVER_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, asQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<XQTGameAreaServerInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<XQTGameAreaServerInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public XQTGameAreaServerInfo queryDetailAreaServerInfo(String id,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_QUERY_DETAIL_AREA_SERVER_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		XQTGameAreaServerInfo info = new XQTGameAreaServerInfo();
		info.setId(id);
		SocketDTO dto = super.send(head, info);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				XQTGameAreaServerInfo info2 = gson.fromJson(body,
						XQTGameAreaServerInfo.class);
				return info2;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<XQTDiscountInfo> queryDiscountInfo(
			XQTDiscountQuery discountQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_QUERY_DISCOUNT_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, discountQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<XQTDiscountInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<XQTDiscountInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<XQTChargetypeInfo> queryChargeTypeInfo(
			XQTChargeTypeQuery chargeTypeQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_QUERY_CHARGE_TYPE_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, chargeTypeQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<XQTChargetypeInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<XQTChargetypeInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public XQTChargetypeInfo queryDetailChargeTypeInfo(String id,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_QUERY_DETAIL_CHARGE_TYPE_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		XQTChargetypeInfo chargetypeInfo = new XQTChargetypeInfo();
		chargetypeInfo.setId(id);
		SocketDTO dto = super.send(head, chargetypeInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				XQTChargetypeInfo info = gson.fromJson(body,
						XQTChargetypeInfo.class);
				return info;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<XQTAccountTypeInfo> queryAccountTypeInfo(
			XQTAccountTypeQuery accountTypeQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_QUERY_ACCOUNT_TYPE_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, accountTypeQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<XQTAccountTypeInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<XQTAccountTypeInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<XQTTopInfo> queryTopInfo(XQTTopQuery topQuery,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_QUERY_TOP_INFO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, topQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<XQTTopInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<XQTTopInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void confirmAudit(String supplierId, int priceRatio,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_CONFIRM_AUDIT);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		XQTDiscountInfo parInfo = new XQTDiscountInfo();
		parInfo.setSupplierId(supplierId);
		parInfo.setPriceRatio(priceRatio);
		parInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, parInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void batchOnline(String goodsState, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_BATCH_ON_OFF_LINE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		XQTParvalueInfo parInfo = new XQTParvalueInfo();
		parInfo.setGoodsState(goodsState);
		parInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, parInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void batchOffline(String goodsState, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_BATCH_ON_OFF_LINE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		XQTParvalueInfo parInfo = new XQTParvalueInfo();
		parInfo.setGoodsState(goodsState);
		parInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, parInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void updateSalePrice(XQTDiscountInfo disInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.XQT_UPDATE_SALE_PRICE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, disInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public XQTOrderQueryInfo orderQuery(String merchantid, String tranid,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020932");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		XQTOrderQueryInfo queryInfo = new XQTOrderQueryInfo();
		queryInfo.setMerchantid(merchantid);
		queryInfo.setTranid(tranid);
		SocketDTO dto = super.send(head, queryInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				XQTOrderQueryInfo info = gson.fromJson(body,
						XQTOrderQueryInfo.class);
				return info;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
